﻿using System;
using System.Web;
using MyWebPagesStarterKit.Controls;
using MyWebPagesStarterKit;
using System.IO;
using System.Web.SessionState;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Drawing;

public partial class SectionControls_Discussion : SectionControlBaseClass
{
    private Discussion _section;
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["antibotimage"] = generateRandomString(4).ToUpper();

            EnsureID();
            valAntiBotImage.ValidationGroup = ID;
            valAntiBotImageRequired.ValidationGroup = ID;

            txtComment.ValidationGroup = ID;
            txtName.ValidationGroup = ID;
            btnAddComment.ValidationGroup = ID;
        }

    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        UpdateViews();
    }

   

    private string generateRandomString(int size)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        return builder.ToString();
    }

    protected void valAntiBotImage_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = (Session["antibotimage"] != null) && (txtAntiBotImage.Text.Trim().ToUpper() == (string)Session["antibotimage"]);
    }

    public override bool HasAdminView
    {
        get { return true; }
    }

    private void UpdateViews()
    {
        if (ViewMode == ViewMode.Edit)
        {
            multiview.SetActiveView(editView);
            txtTitle.Text = _section.Title;
        }
        else
        {
            multiview.SetActiveView(readonlyView);
            litTitle.Text = _section.Title;
            dlComments.DataSource = _section.GetEntries();
            dlComments.DataBind();
            
        }
    }

    protected void Save(object sender, EventArgs e)
    {
        if(txtTitle.Text!= string.Empty)
            _section.Title = txtTitle.Text;
        _section.SaveData();
    }

    protected void AddComment(object sender, EventArgs e)
    {
        Page.Validate(ID);
        if (Page.IsValid)
        {
            if (txtName.Text != string.Empty && txtComment.Text != string.Empty)
            {
                DataRow row = _section.Entries.NewRow();
                row["Guid"] = Guid.NewGuid();
                _section.Entries.Rows.Add(row);
                row["Name"] = SearchResult.RemoveHtml(txtName.Text);
                row["Comment"] = SearchResult.RemoveHtml(txtComment.Text);
                row["DateAdded"] = DateTime.Now;
                row["Tracker"] = Request.ServerVariables["REMOTE_ADDR"];
                row.AcceptChanges();
                _section.SaveData();
                txtName.Text = string.Empty;
                txtComment.Text = string.Empty;
                Response.Redirect(Request.RawUrl);
            }
        }
    }


    public override string InfoUrl
    {
        get { return ""; }
    }

    public override ISection Section
    {
        set
        {
            if (value is Discussion)
                _section = (Discussion)value;
            else
                throw new ArgumentException("Section must be of type Discussion");
        }
        get { return _section; }
    }
}
